<!--
 * @Author: 涵崽
 * @Date: 2023-11-06 15:36:54
 * @email: 1045654@qq.com
 * @gitee: https://gitee.com/han-zai
 * @LastEditors: 涵崽
 * @LastEditTime: 2023-11-10 09:45:02
 * @Description: 登录页-居中布局
-->
<template>
  <div class="login">
    <div class="right">
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        autocomplete="on"
        label-position="left"
      >
        <div class="images">
          <img class="img" v-if="logoUrl" :src="logoUrl" alt="" />
          <!-- <h1 class="img" v-else>Logo</h1> -->
        </div>
        <div class="title-container">
          <h1 class="title">登录</h1>
        </div>

        <el-form-item prop="username">
          <div class="username">
            <span class="svg-container">
              <svg-icon icon-class="user" />
            </span>
            <el-input
              class="input"
              ref="username"
              v-model="loginForm.username"
              placeholder="Username"
              name="username"
              type="text"
              tabindex="1"
              autocomplete="on"
            />
          </div>
        </el-form-item>

        <el-form-item prop="password">
          <div class="pwd">
            <span class="svg-container">
              <svg-icon icon-class="password" />
            </span>
            <el-input
              class="input"
              :key="passwordType"
              ref="password"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="Password"
              name="password"
              tabindex="2"
              autocomplete="on"
              @keyup.native="checkCapslock"
              @keyup.enter.native="handleLogin"
            />

            <span class="show-pwd" @click="showPwd">
              <svg-icon
                :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
              />
            </span>
          </div>
        </el-form-item>
        <div class="btn">
          <el-button
            :loading="loading"
            type="primary"
            style="width: 380px"
            @click.native.prevent="handleLogin"
            >立即登录</el-button
          >
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    const validateUsername = (rule, value, callback) => {
      if (value.length <= 0) {
        callback(new Error("请输入账号"));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error("密码长度不能低于6位"));
      } else {
        callback();
      }
    };
    return {
      logoUrl: "",
      loginForm: {
        username: "admin",
        password: "111111",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", validator: validateUsername },
        ],
        password: [
          { required: true, trigger: "blur", validator: validatePassword },
        ],
      },
      passwordType: "password",
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {},
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        const query = route.query;
        if (query) {
          this.redirect = query.redirect;
          this.otherQuery = this.getOtherQuery(query);
        }
      },
      immediate: true,
    },
  },
  created() {
    // window.addEventListener('storage', this.afterQRScan)
  },
  mounted() {
    if (this.loginForm.username === "") {
      this.$refs.username.focus();
    } else if (this.loginForm.password === "") {
      this.$refs.password.focus();
    }
  },
  destroyed() {
    // window.removeEventListener('storage', this.afterQRScan)
  },
  methods: {
    checkCapslock(e) {
      const { key } = e;
      this.capsTooltip = key && key.length === 1 && key >= "A" && key <= "Z";
    },
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch("user/login", this.loginForm)
            .then(() => {
              this.$router.push({
                path: this.redirect || "/",
                query: this.otherQuery,
              });
              this.loading = false;
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== "redirect") {
          acc[cur] = query[cur];
        }
        return acc;
      }, {});
    },
  },
};
</script>

<style lang="scss" scoped>
.login {
  width: 100vw;
  height: 100vh;
  background: #000;
  background: url("./index2/2.png");
  background-size: 100% 100%;
  .right {
    position: relative;
    height: 100vh;
    .login-form {
      border-radius: 10px;
      width: 640px;
      height: 825px;
      position: absolute;
      // background-color: #fff;
      background: rgba(255, 255, 255, 0.3);
      // 背景的模糊大小通过下面的属性值大小来调制
      backdrop-filter: blur(10px);
      box-shadow: 5px 5px 5px rgba(0, 0, 0, 0.1);
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      .images {
        width: 100%;
        height: 100px;
        text-align: center;
        .img {
          border: 1px solid #000;
          width: 640px;
          height: 100px;
        }
      }
      .title-container {
        width: 100%;
        margin-top: 50px;
        margin-bottom: 50px;
        .title {
          text-align: center;
        }
      }
      .username {
        width: 380px;
        margin: 0 auto;
        .svg-container {
          position: absolute;
          width: 30px;
          z-index: 9;
          top: 0;
          left: 140px;
        }
      }
      .pwd {
        width: 380px;
        margin: 0 auto;
        display: flex;
        justify-content: center;
        align-items: center;
        .svg-container {
          position: absolute;
          width: 30px;
          z-index: 9;
          top: 0;
          left: 140px;
        }
        .show-pwd {
          position: absolute;
          width: 30px;
          z-index: 9;
          top: 0;
          right: 140px;
          cursor: pointer;
        }
      }
      .btn {
        width: 100%;
        display: flex;
        justify-content: center;
        margin-top: 100px;
      }
    }
  }
}
</style>
<style>
.el-input__inner {
  padding-left: 30px !important;
}
.el-form-item__error {
  margin-left: 130px;
}
</style>
